package handler

import (
	"encoding/json"
	"monthly-test4/cache"
	"monthly-test4/model"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 车辆添加

func CarAdd(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")

	car := model.Car{
		CarCode: car_code,
		CarName: car_name,
	}
	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单添加

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	order := model.Order{
		OrderCode: order_code,
		OrderName: order_name,
		Cid:       cid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 车辆订单列表

func CarOrderList(c *gin.Context) {
	/*
		select
		orders.id as OrderId,
		orders.order_code as OrderCode,
		orders.order_name as OrderName,
		cars.car_code as CarCode,
		cars.car_name as CarName
		from orders
		left join cars on cars.id = orders.cid
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var carOrderList []model.CarOrderList

	redis_key := "order_list_" + strconv.Itoa(page)
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheCount == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
			).
			Joins("left join cars on cars.id = orders.cid").
			Offset(offset).Limit(size).Debug().Find(&carOrderList)
		jsonData, _ := json.Marshal(carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheCount), &carOrderList)
	}

	var total int64
	model.DB.Model(&model.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表获取成功",
		"data":  carOrderList,
		"total": total,
	})
	return
}

// 汽车订单详情

func CarOrderSelect(c *gin.Context) {
	/*
			select
			orders.id as OrderId,
			orders.order_code as OrderCode,
			orders.order_name as OrderName,
			cars.car_code as CarCode,
			cars.car_name as CarName
			from orders
			left join cars on cars.id = orders.cid
		    where orders.id = 1
	*/

	id := c.Query("id")

	var carOrderList model.CarOrderList

	redis_key := "order_select_" + id
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheCount == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
			).
			Joins("left join cars on cars.id = orders.cid").
			Where("orders.id = ?", id).
			Find(&carOrderList)
		jsonData, _ := json.Marshal(carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheCount), &carOrderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": carOrderList,
	})
	return
}
